package com.zhdl.hardware.plc.siemens_tcpip.repository.impl;

import com.project.command.faq.event.behavior.BehaviorPerform;
import com.zhdl.common.ytools.callback.CommonCallback;
import com.zhdl.hardware.plc.siemens_tcpip.repository.SiemensTcpPortCmdHandleDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Slf4j
public class SiemensTcpPortCmdHandleDaoImpl implements SiemensTcpPortCmdHandleDao {

    public Map<String, SiemensTcpCmdHandleDaoImpl> daoMap = new ConcurrentHashMap<>();

    @Resource
    public BehaviorPerform behaviorPerform;

    @Resource
    public ThreadPoolTaskScheduler taskScheduler1;

    /**
     * 初始化网口
     *
     * @param port 网口地址
     */
    @Override
    public void addPort(String port) {
        SiemensTcpCmdHandleDaoImpl siemensCmdHandleDao = new SiemensTcpCmdHandleDaoImpl(port, behaviorPerform);
        daoMap.put(port, siemensCmdHandleDao);
    }

    /**
     * 读取plc的tcpip数据
     *
     * @param port     网口Ip
     * @param callback 回调
     */
    @Override
    public void readSiemensTcpIPDate(String port, CommonCallback<String> callback) {
        daoMap.get(port).readSiemensTcpIPDate(callback);
    }
}
